1
        DesiredCapabilities caps = DesiredCapabilities.chrome();
        DRIVER_PATH = FileUtil.getCommonProp("chrome.path");
    System.setProperty("webdriver.chrome.driver",FileUtil.getCommonProp("chrome.driver.path"));

        ChromeOptions options = new ChromeOptions();
      //options.addExtensions(new File("/path/to/extension.crx"))
        options.setBinary(DRIVER_PATH); //注意chrome和chromeDirver的区别
        System.setProperty("webdriver.chrome.driver", DRIVER_PATH);
        options.addArguments("test-type"); //ignore certificate errors
        options.addArguments("headless");// headless mode
        options.addArguments("disable-gpu"); 
//        options.addArguments("log-path=chromedriver.log");
//        options.addArguments("screenshot"); 没打开一个页面就截图
        //options.addArguments("start-maximized"); 最大化
        //Use custom profile
        Map<String, Object> prefs = new HashMap<String, Object>();
//        prefs.put("profile.default_content_settings.popups", 0);
        //http://stackoverflow.com/questions/28070315/python-disable-images-in-selenium-google-chromedriver
        prefs.put("profile.managed_default_content_settings.images",2); //禁止下载加载图片
        options.setExperimentalOption("prefs", prefs);
        
        
        caps.setJavascriptEnabled(true);
        caps.setCapability(ChromeOptions.CAPABILITY, options);        
//        caps.setCapability("takesScreenshot", false);
        
        /* Add the WebDriver proxy capability.
        Proxy proxy = new Proxy();
        proxy.setHttpProxy("myhttpproxy:3337");
        capabilities.setCapability("proxy", proxy);
        */
        WebDriver mDriver = new ChromeDriver(caps);
        

注意:headless模式需要Chrome 59支持,而且windows版尚未支持。

相关配置选项地址参考:
https://github.com/SeleniumHQ...
https://sites.google.com/a/ch...
http://peter.sh/experiments/c...
https://developers.google.com...


xbynet
1k 声望124 粉丝

不雨花犹落,无风絮自飞